// //控制登录页面权限的文件.
// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist
//     //路由守卫
// router.beforeEach(async(to, from, next) => {
//     // start progress bar
//     NProgress.start()

//     // set page title
//     document.title = getPageTitle(to.meta.title)

//     // determine whether the user has logged in
//     const hasToken = getToken()

//     if (hasToken) {
//         if (to.path === '/login') {
//             // if is logged in, redirect to the home page
//             next({ path: '/' })
//             NProgress.done()
//         } else {
//             const hasGetUserInfo = store.getters.name
//             if (hasGetUserInfo) {
//                 next()
//             } else {
//                 try {
//                     // get user info
//                     await store.dispatch('user/getInfo')

//                     next()
//                 } catch (error) {
//                     // remove token and go to login page to re-login
//                     await store.dispatch('user/resetToken')
//                     Message.error(error || 'Has Error')
//                     next(`/login?redirect=${to.path}`)
//                     NProgress.done()
//                 }
//             }
//         }
//     } else {
//         /* has no token*/

//         if (whiteList.indexOf(to.path) !== -1) {
//             // in the free login whitelist, go directly
//             next()
//         } else {
//             // other pages that do not have permission to access are redirected to the login page.
//             next(`/login?redirect=${to.path}`)
//             NProgress.done()
//         }
//     }
// })

// router.afterEach(() => {
//     // finish progress bar
//     NProgress.done()
// })


//控制登录页面登录成功跳转
import store from './store'
import router from './router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

//创建一个白名单,这个白名单就是放不需要token就能访问的页面
let whiteList = ['/login', '/404']
    //路由导航守卫前置钩子
router.beforeEach(async(to, from, next) => {
    //开启进度条
    NProgress.start()
        /* must call `next` */
        //1-判断有无token
        //2-如果有token就跳转到首页,如果没有就跳转到登录页,但是由于login不需要token,这样就会无限的递归,所以需要判断的.
        //store.state.user.token
    if (store.getters.token) {
        //3-判断是否是登录页,如果是跳转到主页,如果不是通过放行
        if (to.path == '/login') {

            next('/')
                // next(form.fullPath)
                // next(to.fullPath)
                // next()
        } else {
            //判断是否有userId如果没有userID就会获取
            if (!store.getters.userInfo.userId) {
                // console.log(1);
                // 如果没有id这个值 才会调用 vuex的获取资料的action
                await store.dispatch('user/getUserInfo')
                let routes = await store.dispatch('permission/getRouters', store.getters.userInfo.roles.menus)
                router.addRoutes(routes)
                    //通过这次不放行,下次一定,确实是可以解决问题,但是404页面没有了
                next(to.path)

            }
            next()
        }
    } else {
        //4-是否在白名单里面,通过放行,如果不是在白名单里面,跳转到登录页面
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next('/login')
        }

    }
});

//路由导航守卫后置
router.afterEach((to, from) => {
    // NProgress.done() // 关闭进度条
});